package com.sayice.coursetable.ui.course.week;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;

import com.sayice.coursetable.R;
import com.sayice.coursetable.databinding.FragmentWeekBinding;
import com.sayice.coursetable.logic.utils.DateUtils;

/**
 * 周课程界面
 */
public class WeekFragment extends Fragment {
    private FragmentWeekBinding binding;
    private WeekViewModel viewModel;

    private WeekCourseAdapter courseAdapter;

    private Integer initialTableId;
    private Integer initialWeek;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = FragmentWeekBinding.inflate(inflater, container, false);
        return binding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        Log.d("WeekFragment", "onViewCreated");
        super.onViewCreated(view, savedInstanceState);
        viewModel = new ViewModelProvider(requireActivity()).get(WeekViewModel.class);
        viewModel.getCourseLiveData().observe(getViewLifecycleOwner(), courses -> {
            Log.d("WeekFragment", "setCourses");
            viewModel.courses.clear();
            viewModel.courses.addAll(courses);
            courseAdapter.notifyItemRangeChanged(0, 42);
        });
        viewModel.getDateLiveData().observe(getViewLifecycleOwner(), dates -> {
            Log.d("WeekFragment", "setDates");
            int size = dates.size();
            if (size == 7) {
                binding.month.setText(dates.get(0)[0] + "\n" + "月");
                for (int i = 0; i < size; i++) {
                    TextView dayTextView = getDayTextView(i + 1);
                    if (dayTextView != null) {
                        dayTextView.setText(DateUtils.toDayText(i + 1) + "\n" + dates.get(i)[1]);
                    }
                }
            }
        });
        if (initialTableId != null && initialWeek != null) {
            Log.d("WeekFragment", "initTable");
            viewModel.updateCourses(initialTableId, initialWeek);
            initialTableId = null;
            initialWeek = null;
        }
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), 7);
        binding.courseList.setLayoutManager(layoutManager);
        courseAdapter = new WeekCourseAdapter(viewModel.courses);
        binding.courseList.setAdapter(courseAdapter);
        // 为当前星期添加边框
        TextView dayTextView = getDayTextView(DateUtils.getWeekDayNum());
        if (dayTextView != null) {
            dayTextView.setBackgroundResource(R.drawable.text_view_background);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }

    /**
     * 更新课程
     *
     * @param tableId 课表编号
     * @param week    周次
     */
    public void updateCourses(int tableId, int week) {
        if (viewModel == null) {
            initialTableId = tableId;
            initialWeek = week;
        } else {
            viewModel.updateCourses(tableId, week);
        }
    }

    /**
     * 切换周次
     *
     * @param tableId 课表编号
     * @param week    周次
     */
    public void changeWeek(int tableId, int week) {
        if (viewModel == null) {
            initialTableId = tableId;
            initialWeek = week;
        } else {
            viewModel.updateCourses(tableId, week);
            viewModel.updateDates(week);
        }
    }

    /**
     * 获取显示星期的 TextView
     *
     * @param dayNum 星期号（星期一为 1）
     */
    private TextView getDayTextView(int dayNum) {
        switch (dayNum) {
            case 1:
                return binding.day1;
            case 2:
                return binding.day2;
            case 3:
                return binding.day3;
            case 4:
                return binding.day4;
            case 5:
                return binding.day5;
            case 6:
                return binding.day6;
            case 7:
                return binding.day7;
            default:
                return null;
        }
    }
}



